package com.bawei.newsapplication.model.http
import com.bawei.newsapplication.model.api.ApiServer
import com.blankj.utilcode.util.SPUtils
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
import java.util.concurrent.TimeUnit
/**
 * @author : 谢庆平
 * @date   : 2024/10/15 19:10
 * @desc   :网络工具类
 */
class HttpUtils private constructor(){
//TODO 定义网络工具类的单例模式
    companion object{
        val http:HttpUtils by lazy(mode =LazyThreadSafetyMode.SYNCHRONIZED){
            HttpUtils()
        }
    }
    //TODO 定义连接和读取超时 时间 retrofit
    var retention= Retrofit.Builder()
        .client(OkHttpClient.Builder()
            .readTimeout(5,TimeUnit.SECONDS)//TODO 读取超时
            .connectTimeout(5,TimeUnit.SECONDS)//TODO 连接超时
            .addInterceptor(HttpLoggingInterceptor()
                .setLevel(HttpLoggingInterceptor.Level.BODY))//TODO 打印网络日志
            .addInterceptor(object : Interceptor {
                override fun intercept(chain: Interceptor.Chain): Response {
                    var userId = SPUtils.getInstance().getString("userId")
                    var token = SPUtils.getInstance().getString("token")
                    val request = chain.request().newBuilder()
                        .addHeader("userId", userId)
                        .addHeader("token", token)
                        .build()
                    return chain.proceed(request)
                }
            })
            .build())
        .baseUrl("http://10.59.9.18/")//TODO 获取网络接口头参
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build()
//TODO 调用Api网络接口数据
    var apiServer=retention.create(ApiServer::class.java)




}